package logic

import (
	"context"

	"github.com/tal-tech/go-zero/core/logx"
	"monitor/user/api/internal/svc"
	"monitor/user/api/internal/types"
	"monitor/user/model"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) UserInfoLogic {
	return UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo(token string) (*types.UserReply, error) {
	// todo: add your logic here and delete this line
	claims := ParseToken(token)
	userId := claims["id"].(float64)

	userInfo, err := l.svcCtx.UserModel.FindOne(int64(userId))
	switch err {
	case nil:
		return &types.UserReply{
			Id:       userInfo.Id,
			Username: userInfo.Name,
			Mobile:   userInfo.Mobile,
		}, nil
	case model.ErrNotFound:
		return nil, errorUserNotFound
	default:
		return nil, err
	}
}
